import requests
from tool import tms
import json

url = "https://api.deepseek.com/chat/completions"
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer sk-3c8ae8738d91498bbdcffc76bd0345b1'
}
payload = {
    "messages": [

        {
            "content": "",
            "role": "user"
        }
    ],
    "model": "deepseek-chat",
    "frequency_penalty": 0,
    "max_tokens": 2048,
    "presence_penalty": 0,
    "response_format": {
        "type": "text"
    },
    "stop": None,
    "stream": False,
    "stream_options": None,
    "temperature": 1,
    "top_p": 1,
    "tools": None,
    "tool_choice": "none",
    "logprobs": False,
    "top_logprobs": None
}

import time
def aaa():
    sql= "select id,title from campaign_YN where CN_title_from_DS is null and conversions>0"
    st_time=time.time()
    
    while True:
        rs=tms.ExecQuery(sql)
        if len(rs)==0:
            break
        total_count=len(rs)
        for pid,tit in rs:
            question=f'{tit} 请用中文告诉我这个产品是什么，8个字以内'

            payload['messages'][0]['content']=question
            try:
                res = requests.request("POST", url, headers=headers, json=payload,timeout=60)
                js_data=res.json()
                cot=js_data['choices'][0]['message']['content']
                aff=tms.ExecNoQuery('update campaign_YN set CN_title_from_DS=? where id=?',(cot,pid))
                print(f'id:{pid},翻译长度:{len(cot)},更新状态:{aff},剩余任务队列:{total_count}')

            except Exception as e:
                print(e)
            total_count-=1
        et_time=time.time()
        print(f'耗时:{et_time-st_time}秒')

def bbb():
    session=requests.session()

    session.headers={
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'Bearer sk-3c8ae8738d91498bbdcffc76bd0345b1'
    }

    pps=tms.ExecQuery('select id,title from campaign_YN where CN_title_from_DS is null and conversions>0')
    st_time=time.time()
    for tit,kw in pps:
        question=f'{tit} 请用中文告诉我这个产品是什么，8个字以内'
        
        payload['messages'][1]['content']=question
        res = session.request("POST", url, json=payload)
        print(res.status_code)
        js_data=res.json()
        print(js_data)
    et_time=time.time()
    print(f'耗时:{et_time-st_time}秒')
    


